﻿using PracticeCentre.Common;
using PracticeCentre.DataControl;
using PracticeCentre.Modeles;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;

namespace PracticeCentre.Business
{
    public class Employees
    {
        /// <summary>
        /// 根据员工编号
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public Employee GetModelByJobNumber(Employee model)
        {
            DataControlHelper dataHelper = new DataControlHelper();
            try
            {
                SqlParameter[] parameters = new SqlParameter[] {
                    new SqlParameter("@JobNumber",model.JobNumber)
                };
                DataModelHandler<Employee> modeHandler = new DataModelHandler<Employee>();
                List<Employee> employeeList = modeHandler.FillModel(dataHelper.ExecuteDataTable(ProceduresConfig.GetEmployeeModel, parameters));
                return employeeList.Count > 0 ? employeeList[0] : null;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 添加新员工
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Add(Employee model)
        {
            DataControlHelper dataHelper = new DataControlHelper();
            try
            {
                SqlParameter[] parameters = new SqlParameter[] {
                    new SqlParameter("@JobNumber",model.JobNumber),
                    new SqlParameter("@PersonalId",model.PersonalId),
                    new SqlParameter("@DepartmentNumber",model.DepartmentNumber),
                    new SqlParameter("@PositionNumber",model.PositionNumber)
                };
                return dataHelper.ExecuteNoQuery(ProceduresConfig.AddEmployees, parameters);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 更新员工信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Update(Employee model)
        {
            DataControlHelper dataHelper = new DataControlHelper();
            try
            {
                SqlParameter[] parameters = new SqlParameter[] {
                    new SqlParameter("@Id",model.Id),
                    new SqlParameter("@JobNumber",model.JobNumber),
                    new SqlParameter("@PersonalId",model.PersonalId),
                    new SqlParameter("@DepartmentNumber",model.DepartmentNumber),
                    new SqlParameter("@PositionNumber",model.PositionNumber)
                };
                return dataHelper.ExecuteNoQuery(ProceduresConfig.UpdateEmployees, parameters);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 删除一个员工信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Delete(Employee model)
        {
            DataControlHelper dataHelper = new DataControlHelper();
            try
            {
                SqlParameter[] parameters = new SqlParameter[] {
                    new SqlParameter("@JobNumber",model.JobNumber)
                };
                return dataHelper.ExecuteNoQuery(ProceduresConfig.DeleteEmployees, parameters);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}
